package com.leesche.zcsp.node.app.v1.controller.operate;

import com.leesche.zcsp.node.app.v1.controller.admin.AppAdminController;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.model.PubPlace;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubPlaceService;
import com.leesche.zcsp.node.web.service.impl.data.read.InfoDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.PubPlaceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubPlaceCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/11/15.
 * 场地模块
 */
@Controller
@RequestMapping("/app/v1/operate/place")
public class AppPlaceController extends AppAdminController {
    private final static Logger LOGGER = LoggerFactory.getLogger(AppPlaceController.class);

    @Autowired
    private PubPlaceService placeService;
    @Autowired
    private PubDeviceService deviceService;

    /**
     * 场地列表
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listView", method = RequestMethod.POST)
    public Map<String,Object> listView(
        QueryPubPlaceCondition condition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            condition.setAdminId(adminId);
            List<PubPlaceDto> list = placeService.findPubPlaceList(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 添加场地
     * @param place
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Map<String,Object> add(
            PubPlace place
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            place.setAdminid(adminId);
            place.setState(1);
            placeService.insert(place);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 单个场地
     * @param placeCondition
     * @param deviceCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editView", method = RequestMethod.POST)
    public Map<String,Object> editView(
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition
    ){
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            PubPlaceDto dto = placeService.findByCondition(placeCondition);
            map.put("place",dto);

            deviceCondition.setAdminId(Long.valueOf(dto.getAdminid()));
            List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);

            //礼品机
            List<InfoDeviceDto> giftList = new ArrayList<InfoDeviceDto>();
            //兑币机
            List<InfoDeviceDto> currencyList = new ArrayList<InfoDeviceDto>();
            //娃娃机
            List<InfoDeviceDto> dollList = new ArrayList<InfoDeviceDto>();
            //礼品兑换机
            List<InfoDeviceDto> giftExchangeList = new ArrayList<InfoDeviceDto>();

            for(InfoDeviceDto deviceDto : deviceList){
                switch (deviceDto.getDeviceTypeId().intValue()){
                    //礼品
                    case 1:
                        giftList.add(deviceDto);
                        break;
                    //兑币
                    case 2:
                        currencyList.add(deviceDto);
                        break;
                    //娃娃
                    case 3:
                        dollList.add(deviceDto);
                        break;
                    //礼品兑换
                    case 4:
                        giftExchangeList.add(deviceDto);
                        break;
                    default:
                        break;
                }
            }

            map.put("currencyList",currencyList);
            map.put("dollList",dollList);
            map.put("giftList",giftList);
            map.put("giftExchangeList",giftExchangeList);

            return WebResult.success().put("data",map).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改场地
     * @param placeId
     * @param address
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Map<String,Object> edit(
            Long placeId,
            String address
    ){
        try {
            PubPlace oldPlace = placeService.selectByPrimaryKey(placeId);
            oldPlace.setAddress(address);
            placeService.updateByPrimaryKeySelective(oldPlace);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备场地列表
     * @param placeCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDevicePlaceView", method = RequestMethod.POST)
    public Map<String,Object> editDevicePlaceView(
            QueryPubPlaceCondition placeCondition
    ){
        try {
//            Long adminId = 1L;
            Long adminId = getAdminId();
            placeCondition.setAdminId(adminId);

            PubPlaceDto placeDto = placeService.findDevicePlace(placeCondition);

            List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);

            for(PubPlaceDto dto : placeList){
                if(dto.getPlaceid() == placeDto.getPlaceid()){
                    dto.setState(1);
                }else{
                    dto.setState(0);
                }
            }

            return WebResult.success().put("data",placeList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备修改场地操作
     * @param deviceId 设备id
     * @param placeId 场地id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDevicePlace", method = RequestMethod.POST)
    public Map<String,Object> editDevicePlace(
            Long deviceId,
            Long placeId
    ){
        try {
            PubDevice pubDevice = deviceService.selectByPrimaryKey(deviceId);
            pubDevice.setPlaceid(placeId);
            deviceService.updateByPrimaryKeySelective(pubDevice);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

}
